package com.zp.study.scloud.oauth2.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;

/**
 * 配置认证服务器
 */
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        // 配置客户端
        clients
                .inMemory()// 使用内存设置
                // 为客户端分配 client_id
                .withClient("client_id_1")
                // 为客户端分配 client_secret
                .secret(passwordEncoder.encode("client_id_1_secret"))
                // 授权模式：授权码模式
                .authorizedGrantTypes("authorization_code")//这里可以填写多个模式
                // 授权范围 TODO ??
                .scopes("app")
                // code回调地址
                .redirectUris("http://localhost:8071/test/oauth2/authCodeCallBack");
    }
}